<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

/**
 * Class BakController 后台数据备份还原控制器
 * @package App\Http\Controllers\admin
 */
class BakController extends AdminPlatformController
{
    /** 属性声明 */
    private $table_list = null;

    /** 方法声明 */
    /**
     * 构造方法
     */
    public function __construct(Route $route){
        parent::__construct($route);
        /** 初始化表集合 */
        $this -> init_table_list();
    }

    /**
     * 加载控制面板视图
     */
    public function index(){
        return view('admin.bak.bak');
    }

    /**
     * 备份全站数据
     */
    public function bakAll(){
        /** 初始化保存url */
        $url = app_path() . '/bak/';
        $url = str_replace('\\','/',$url);
        /** 循环所有表 */
        foreach($this->table_list as $table){
            $file  = $url . $table->Tables_in_gshop . ".bak";
            //判断文件是否已经备份
            if( file_exists($file) ){
                if(isset($_POST['act'])){
                    $res = unlink($file);
                }else{
                    $msg = [
                        'status' => 0,
                        'msg' => "您已经备份过了，是否覆盖原有备份文件?"
                    ];
                    return $msg;
                }
            }
            //执行写入备份数据
            $sql = "select * into outfile "
                ."'". $url . $table->Tables_in_gshop
                . ".bak" ."'" . " from " . $table->Tables_in_gshop;
            DB::statement($sql);
        }
        $msg = [
            'status' => 1,
            'msg' => "备份成功!"
        ];
        return $msg;
    }

    /**
     * 初始化数据库表集合
     */
    public function init_table_list(){
        $this -> table_list = DB::select('show tables');
    }

    /**
     * 自动还原数据
     */
    public function gostAll(){
        if(!$input = Input::all()){
            return view('admin.bak.gost');
        }
        //检测是否已有备份
        $dir = app_path() . "/bak/";
        $dir = str_replace('\\','/',$dir);
        $count = 0;
        $dh = opendir($dir);
        while($dir = readdir($dh)){
            $count++;
        }
        if($count==2){
            $data = [
                'status' => 0,
                'msg' => '很抱歉!您还没有备份呢!'
            ];
            return $data;
        }
        /** 清空所有表 */

        foreach($this -> table_list as $table){
            $tablename = $table -> Tables_in_gshop;
            $sql = "truncate $tablename";
            /** 清空数据库 */
            DB::statement($sql);
            /** 重新写入数据 */
            $dir = app_path() . "/bak/";
            $dir = str_replace('\\','/',$dir);

            $file = $dir . $tablename . '.bak';
            $sql = "load data infile '" . $file . "' into table " . $tablename;
            $link = mysqli_connect('127.0.0.1','root','root','gshop');
            mysqli_query($link,$sql);
        }
        $data = [
            'status' => 1,
            'msg' => '全站还原成功!'
        ];
        return $data;
    }
}
